#!/bin/bash
#########################################################
#                                                       #
#           Pi-Star HostAPd Service Handler             #
#                                                       #
# Written for Pi-Star (http://www.mw0mwz.co.uk/pi-star) #
#               By Andy Taylor (MW0MWZ)                 #
#                                                       #
#                     Version 1.5                       #
#                                                       #
#########################################################

# Service Config
DAEMON=hostapd
DAEMON_PATH=/usr/sbin/
DAEMON_OPTS=" -B -P /run/pistar-hostapd.pid /etc/hostapd/hostapd.conf"
PGREP=/usr/bin/pgrep
KILL=/bin/kill
SLEEP=/bin/sleep
DNSMASQDAEMON=dnsmasq

# Pre-flight checks...
test -x ${DAEMON_PATH}${DAEMON} || exit 0
test -f "/etc/hostapd/hostapd.conf" || exit 0
if [ -f "/etc/hostap.off" ]; then
    exit 0
fi

# Check that WLAN0 exists
if [ ! -d "/sys/class/net/wlan0" ] && [ ! -d "/sys/class/net/wlan0_ap" ]; then
	exit 0
fi

# Check if the Wireless network is UP, and die if it IS
if [ -d "/sys/class/net/wlan0" ]; then
	if [ `cat /sys/class/net/wlan0/operstate` == "up" ]; then
		exit 0
	fi
fi

# Make sure we have a card that supports AP mode
if [ `grep -c -E '(ath5k |ath9k |ath9k_htc |b43 |b43legacy |brcmfmac |carl9170 |cw1200 |iwlwifi |libertas_tf |mac80211_hwsim |mwifiex |mwl8k |p54spi |p54usb |rt73usb |rt2500usb |rt2800usb |vt6656 |wil6210 |wl12xx |zd1211rw |dhd |xradio_wlan |8188eu )' /proc/modules` -eq "0" ]; then
	exit 0
fi

# Some cards cannot be in AP mode *AND* Managed mode at the same time
if [ `grep -c -E '(rt2800usb )' /proc/modules` -gt "0" ]; then
	ifconfig wlan0 down
	${SLEEP} 2
fi

# Some cards need special attention
if [ `grep -c -E '(8188eu )' /proc/modules` -gt "0" ]; then
	if [ ! -d "/sys/class/net/wlan0_ap" ]; then
		mount -o remount,rw /
		sed -i '/hwaddress ether b8:27:eb:00:00:01/d' /etc/network/interfaces
		ifconfig wlan0 down
		ip link set wlan0 name wlan0_ap
		${SLEEP} 2
	fi
fi

# FriendlyArm Nano Pi Neo Air
if [ `grep -c -E '(dhd )' /proc/modules` -gt "0" ]; then
	if [ ! -d "/sys/class/net/wlan0_ap" ]; then
		mount -o remount,rw /
		sed -i '/hwaddress ether b8:27:eb:00:00:01/d' /etc/network/interfaces
		ifconfig wlan0 down
		rmmod dhd
		modprobe dhd op_mode=2
		ip link set wlan0 name wlan0_ap
		${SLEEP} 2
	fi
fi

# Orange Pi Zero XRadio Fixes
if [ `grep -c -E '(xradio_wlan )' /proc/modules` -gt "0" ]; then
	if [ ! -d "/sys/class/net/wlan0_ap" ]; then
		mount -o remount,rw /
		sed -i '/hwaddress ether b8:27:eb:00:00:01/d' /etc/network/interfaces
		${SLEEP} 2
	fi
fi

# Autofix DNSMASQ
if [ `grep -c -E 'DNSMASQ_EXCEPT' /etc/default/dnsmasq` -eq "0" ]; then
	mount -o remount,rw /
	echo "DNSMASQ_EXCEPT=lo" >> /etc/default/dnsmasq
fi

case "$1" in
	start)
		mount -o remount,rw /
		if [ $(grep /var/lib/misc /proc/mounts | wc -l) -eq 0 ]; then
			mount -t tmpfs -o nodev,noatime,nosuid,mode=1777,size=16k tmpfs /var/lib/misc
		fi
		sysctl net.ipv4.ip_forward=1
		iw dev wlan0 interface add wlan0_ap type __ap
		${SLEEP} 4 && ifup wlan0_ap && ${SLEEP} 3
		if [ `${PGREP} ${DAEMON}` ]; then
			echo -e "$DAEMON is already running as PID "`$PGREP $DAEMON`
		else
			systemctl start hostapd
			echo -e "$DAEMON started as PID "`$PGREP $DAEMON`
		fi
		if [ `${PGREP} ${DNSMASQDAEMON}` ]; then
			echo -e "$DNSMASQDAEMON is already running as PID "`$PGREP $DNSMASQDAEMON`
		else
			systemctl start dnsmasq
			echo -e "$DNSMASQDAEMON started as PID "`$PGREP $DNSMASQDAEMON`
		fi
		mount -o remount,ro /
		exit 0
		;;
	stop)
		mount -o remount,rw /
		sysctl net.ipv4.ip_forward=0
		if [ `${PGREP} ${DNSMASQDAEMON}` ]; then
			echo -e "Killing $DNSMASQDAEMON PID "`$PGREP $DNSMASQDAEMON`
			systemctl stop dnsmasq
		else
			echo -e "$DNSMASQDAEMON is not running"
		fi
		if [ `${PGREP} ${DAEMON}` ]; then
			echo -e "Killing $DAEMON PID "`$PGREP $DAEMON`
			systemctl stop hostapd
			${SLEEP} 5
		else
			echo -e "$DAEMON is not running"
		fi
		ifdown wlan0_ap
		iw dev wlan0_ap del
		mount -o remount,ro /
		exit 0
		;;
	restart)
		mount -o remount,rw /
		sysctl net.ipv4.ip_forward=0
		if [ `${PGREP} ${DNSMASQDAEMON}` ]; then
			echo -e "Killing $DNSMASQDAEMON PID "`$PGREP $DNSMASQDAEMON`
			systemctl stop dnsmasq
		else
			echo -e "$DNSMASQDAEMON is not running"
		fi
		if [ `${PGREP} ${DAEMON}` ]; then
			echo -e "Killing $DAEMON PID "`$PGREP $DAEMON`
			systemctl stop hostapd
			${SLEEP} 5
		else
			echo -e "$DAEMON is not running"
		fi
		ifdown wlan0_ap
		iw dev wlan0_ap del
		
		${SLEEP} 2
		
		sysctl net.ipv4.ip_forward=1
		iw dev wlan0 interface add wlan0_ap type __ap
		${SLEEP} 3 && ifup wlan0_ap && ${SLEEP} 2
		if [ `${PGREP} ${DAEMON}` ]; then
			echo -e "$DAEMON is already running as PID "`$PGREP $DAEMON`
		else
			systemctl start hostapd
			echo -e "$DAEMON started as PID "`$PGREP $DAEMON`
		fi
		if [ `${PGREP} ${DNSMASQDAEMON}` ]; then
			echo -e "$DNSMASQDAEMON is already running as PID "`$PGREP $DNSMASQDAEMON`
		else
			systemctl start dnsmasq
			echo -e "$DNSMASQDAEMON started as PID "`$PGREP $DNSMASQDAEMON`
		fi
		mount -o remount,ro /
		exit 0
		;;
	status)
		if [ `${PGREP} ${DAEMON}` ]; then
			echo -e "$DAEMON is running as PID "`${PGREP} ${DAEMON}`
		else
			echo -e "$DAEMON is not running"
		fi
		if [ `${PGREP} ${DNSMASQDAEMON}` ]; then
			echo -e "$DNSMASQDAEMON is running as PID "`${PGREP} ${DNSMASQDAEMON}`
		else
			echo -e "$DNSMASQDAEMON is not running"
		fi
		exit 0
		;;
	*)
		echo $"Usage: $0 {start|stop|status}"
		exit 0
esac
